﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UpdatePro
{
    public partial class FrmUpdate : Form
    {
        public UpdateCore updateCore = null;
        public FrmUpdate()
        {
            InitializeComponent();
            //初始化
            Init();
        }

        private void Init()
        {
            //禁用相关的按钮
            this.btnFinish.Enabled = false;
            this.btnCancel.Enabled = false;
            updateCore = new UpdateCore();
            //委托关联
            updateCore.showPrecent = ShowPrecent;

            this.lblVersion.Text = updateCore.LastUpdateInfo.Version;
            this.lblLastUpdateTime.Text = updateCore.LastUpdateInfo.UpdateTime.ToString();

            //更新文件列表
            var filelist = updateCore.NowUpdateInfo.FileList;
            foreach (var item in filelist)
            {
                this.lvUpdateList.Items.Add(new ListViewItem(item));
            }

        }

        /// <summary>
        /// 下载进度展示
        /// </summary>
        /// <param name="fileIndex"></param>
        /// <param name="precent"></param>
        public void ShowPrecent(int fileIndex, int finishedPrecent)
        {
            this.lvUpdateList.Items[fileIndex].SubItems[3].Text = finishedPrecent + "%";
            this.pbDownLoadFile.Maximum = 100;
            this.pbDownLoadFile.Value = finishedPrecent;
            if (fileIndex < this.lvUpdateList.Items.Count-1 && finishedPrecent == 100)
            {
                this.pbDownLoadFile.Value = 0;
            }
        }

        #region 窗体移动代码
        private Point mouseOff;//鼠标移动位置变量
        private bool leftFlag;//鼠标是否为左键
        private void Frm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y);//获取移动变量的值
                leftFlag = true;//点击左键按下时标注为true
            }
        }

        private void Frm_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后leftFalg为false
            }
        }

        private void Frm_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);//设置移动的位置
                Location = mouseSet;
            }
        }
        #endregion

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFinish_Click(object sender, EventArgs e)
        {
            //文件拷贝到运行目录
            try
            {
                if (updateCore.FileCopy())
                {
                    Application.ExitThread();
                    Application.Exit();
                    Process.Start("SMDB.exe");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("升级异常：" + ex.Message);
            }
        }

        /// <summary>
        /// 下一步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNext_Click(object sender, EventArgs e)
        {
            //更新按钮状态
            this.btnCancel.Enabled = true;
            this.btnNext.Enabled = false;
            try
            {
                this.lblUpdateStatus.Text = "正在下载更新文件，请稍后...";
                this.lblTips.Text = "点击'取消'可以结束升级...";
                //开始下载文件：
                updateCore.DownLoadUpdateFileList();
                this.lblTips.Text = "全部文件已经下载，点击'完成'完成升级";
                this.lblUpdateStatus.Visible = false;
                this.btnFinish.Enabled = true;
                this.btnCancel.Visible = false;
                this.btnNext.Visible = false;
                this.btnFinish.Location = this.btnCancel.Location;
            }
            catch (Exception ex)
            {
                this.btnNext.Enabled = true;
                this.btnCancel.Enabled = false;
                MessageBox.Show("升级异常：" + ex.Message);
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认取消升级吗", "询问提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                //关闭升级程序
                Application.ExitThread();
                Application.Exit();
            }
        }
    }
}
